﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgriculturalMachinery.Models;
using System.IO;

namespace AgriculturalMachinery.Controllers
{
    public class PersonalController : Controller
    {
        int id = 0;
        //
        // GET: /Personal/

        public ActionResult Index()
        {
            return View(new HomeViewModel());
        }

        //
        // Get: /Personal/MyInfo/3
        public ActionResult MyInfo(int id)
        {
            this.id = id;
            HomeViewModel hvm = new HomeViewModel();
            AccountRepository ar = new AccountRepository();
            hvm.CurrentClient = ar.FindUserByID(id);

            return View("index", hvm);
        }

        //
        // GET: /Personal/Detail/3
        public ActionResult Detail(int id)
        {
            this.id = id;
            return View(new PurchaseViewModel<object>(id));
        }

        //
        public ActionResult UploadFile(int id)
        {
            foreach (string upload in Request.Files)
            {
                if (!Request.Files[upload].HasFile())
                    continue;
                string path = AppDomain.CurrentDomain.BaseDirectory + "avatar/";
                string FileName = Path.GetFileName(Request.Files[upload].FileName);
                Request.Files[upload].SaveAs(Path.Combine(path, FileName));

                AccountRepository ar = new AccountRepository();
                ar.ChangeAvatar(id, FileName);
                
            }
            return Detail(id);
        }
    }

    public static class exHttpPostedFileBase
    {

        public static bool HasFile(this HttpPostedFileBase file)
        {
            return (file != null && file.ContentLength > 0) ? true : false;
        }
    }
}
